
import React from 'react'
import ReactDOM from 'react-dom'

import * as UnitAction  from '../../actions/UnitAction';
import { connect }  from 'react-redux';

class UserSettingCreate extends React.Component {
  constructor(props) {
    super(props)

    this.state = {
      data: {},
      showList: false
    }
  }

  componentDidMount() {
    this.$unitList = $(ReactDOM.findDOMNode(this.refs.userCreateUnitLists));
    this.$expire_date = $(ReactDOM.findDOMNode(this.refs.expire_date))
    // $(this.refs.UserStartTime).datetimepicker({
    //   format: 'HH:mm',
    //   inline: true
    // });
  }

  componentDidUpdate () {
    var units = this.props.units;
    var self = this;
    if (units && units.success) {
      if (units.data.length == 0 || !this.state.showList) {
        this.$unitList.hide();
      }else if (!this.state.data.unit_desc || this.state.data.unit_desc.trim() == ''){
        this.$unitList.hide();
      }
      else {
        this.$unitList.show();
      }
    }else {
      this.$unitList.hide();
    }
  }

  onChange(event, key) {
    var value = $(event.target).val();

    if(key == 'mobile'){
      console.log("in");
      if(value.length > 11){
        value = value.substr(0,11);
      }
    }

    var data = this.state.data;
    data[key] = value;
    this.setState({
      data: data
    })
  }

  onDataSubmit() {
    var data = this.state.data;
    var allEmtpy = this.checkEmpty(this.state.data.expire_year) && this.checkEmpty(this.state.data.expire_month)
                && this.checkEmpty(this.state.data.expire_day);
    var allNotEmtpy = (!this.checkEmpty(this.state.data.expire_year)) && (!this.checkEmpty(this.state.data.expire_month))
                && (!this.checkEmpty(this.state.data.expire_day))


    if (allEmtpy || allNotEmtpy) {
      if (allNotEmtpy) {
        var expire_date = this.state.data.expire_year + "-" + this.state.data.expire_month + "-"
                          + this.state.data.expire_day + " 00:00:00";
        data.expire_date = new Date(expire_date)
        if (isNaN(data.expire_date.getTime())) {
          alert("请输入正确的日期!")
          return;
        }
      }else {
        data.expire_date = null;
      }

      if (isNaN(data.mobile)) {
        alert("请输入正确的电话")
        return
      }

      this.props.onDataCreate(data)
      this.setState({
        showList: false,
        data: {}
      })
    }else {
      alert("请输入完整日期!")
    }

  }

  pad(n) {
    return n < 10 ? "0"+n : n;
  }

  checkEmpty(text) {
    return !text || text.length == 0
  }

  onUnitNumberChange(event) {
    var value = $(event.target).val();
    var data = this.state.data;
    data.unit_desc = value;
    this.setState({
      data: data,
      showList: true
    })
    this.props.dispatch(
      UnitAction.loadUnitsAsync({unit_desc: value})
    )
  }

  onUnitNumberClick(event) {
    var value = $(event.target).html();
    var data = this.state.data;
    data.unit_desc = value;
    this.setState({
      data: data,
      showList: false
    })
  }

  renderUnitList() {
    var units = this.props.units
    var self = this;
    if (units && units.success) {
      if (units.data.length == 0 || !this.state.showList) {
        return ""
      }else if (!this.state.data.unit_desc || this.state.data.unit_desc.trim() == ''){
        return ""
      }
      else {
        return units.data.map(function(data) {
          return (
            <li className="list-group-item"
                onClick={(event)=>self.onUnitNumberClick(event)}>{data.unit_desc}</li>
          )
        })
      }
    }else {
      return "";
    }
  }

  render() {
    var self = this;
    var bind_date = "",
        expire_date = "";
    if (this.state.data.expire_date) {
      var date = new Date(this.state.expire_date);
      expire_date = date.getFullYear() + "-" + this.pad(date.getMonth() + 1) + "-"
                        + this.pad(date.getDate()) +"T"+"00:00:00";
    }

    return (
      <div className="modal fade" id="UserSettingCreate" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div className="modal-dialog" role="document">
          <div className="modal-content">
            <div className="modal-header">
              <button type="button" className="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 className="modal-title" id="User-modal-label">业主详情</h4>
            </div>
            <div className="modal-body">
              <div className="form-group">
                <label for="User-name">名字</label>
                <input ref="UserName" type="text" className="form-control"
                        id="User-name" placeholder="名字" value={this.state.data.name}
                        onChange={(event)=>this.onChange(event, "name")}/>
              </div>
              <div className="form-group">
                <label for="User-phone">联系方式</label>
                <input ref="UserPhone" type="text" className="form-control"
                        id="User-phone" placeholder="联系方式" value={this.state.data.mobile}
                        onChange={(event)=>this.onChange(event, "mobile")}/>
              </div>
              <div className="form-group">
                <label for="User-phone">户号</label>
                <input ref="UserPhone" type="text" className="form-control"
                        id="User-phone" placeholder="户号" value={this.state.data.unit_desc}
                        onChange={(event)=>this.onUnitNumberChange(event)}/>
                <div className="unit-lists" ref="userCreateUnitLists">
                  <ul className="list-group">
                    {this.renderUnitList()}
                  </ul>
                </div>
              </div>
              <div className="form-group">
                <label for="User-phone">账单ID</label>
                <input ref="UserPhone" type="text" className="form-control"
                        id="User-phone" placeholder="账单ID" value={this.state.data.reg_code}
                        onChange={(event)=>this.onChange(event, "reg_code")}/>
              </div>
              <div className="form-group">
                <label for="User-phone">到期时间</label>
                <div className="row">
                  <input ref="PropertyStartTime" type="text" className="col-sm-1"
                          id="Property-phone" placeholder="年" value={self.state.data.expire_year}
                          onChange={(event)=>this.onChange(event, "expire_year")}/>
                  <span className="col-sm-1">年</span>
                  <input ref="PropertyStartTime" type="text" className=" col-sm-1"
                          id="Property-phone" placeholder="月" value={self.state.data.expire_month}
                          onChange={(event)=>this.onChange(event, "expire_month")}/>
                  <span className="col-sm-1">月</span>
                  <input ref="PropertyStartTime" type="text" className=" col-sm-1"
                          id="Property-phone" placeholder="日" value={self.state.data.expire_day}
                          onChange={(event)=>this.onChange(event, "expire_day")}/>
                  <span className="col-sm-1">日</span>
                </div>
              </div>
            </div>
            <div className="modal-footer">
              <button type="button" className="btn btn-default" data-dismiss="modal">关闭</button>
              <button type="button" className="btn btn-primary"
                      onClick={(event)=> this.onDataSubmit()}>提交</button>
            </div>

          </div>
        </div>
      </div>
    )
  }
}

const mapStateToProps = (state, ownProps) => {
  return {
    units: state.units
  }
}

export default connect(mapStateToProps)(UserSettingCreate)
